/*
Package requests
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/3/26 18:20
@File     : login_request.go
@Project  : gohub
@Desc     :
*/
package requests

import (
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
	"gohub/app/requests/validators"
)

type LoginByPhoneRequest struct {
	Phone      string `json:"phone,omitempty" valid:"phone"`
	VerifyCode string `json:"verify_code,omitempty" valid:"verify_code"`
}

func LoginByPhone(data any, c *gin.Context) map[string][]string {
	//验证规则
	rules := govalidator.MapData{
		"phone":       []string{"required", "digits:11"},
		"verify_code": []string{"required", "digits:6"},
	}
	//错误消息提示
	messages := govalidator.MapData{
		"phone": []string{
			"required:手机号必填，参数名 phone",
			"digits:手机号码必须为11位",
		},
		"verify_code": []string{
			"required:验证码答案必填",
			"digits:验证码长度不对",
		},
	}

	errs := validate(data, rules, messages)

	//手机验证码
	_data := data.(*LoginByPhoneRequest)
	errs = validators.ValidateVerifyCode(_data.Phone, _data.VerifyCode, errs)
	return errs
}

// LoginByPasswordRequest
// @Description: 账号密码登录结构体
type LoginByPasswordRequest struct {
	CaptchaID     string `json:"captcha_id,omitempty" valid:"captcha_id"`
	CaptchaAnswer string `json:"captcha_answer,omitempty" valid:"captcha_answer"`

	LoginID  string `json:"login_id" valid:"login_id"`
	Password string `json:"password,omitempty" valid:"password"`
}

/**
 * LoginByPassword
 * @Description: 账密登录图片验证码验证
 * @param data
 * @param c
 * @return map[string][]string
 */
func LoginByPassword(data any, c *gin.Context) map[string][]string {
	//验证规则
	rules := govalidator.MapData{
		"login_id":       []string{"required", "min:3"},
		"password":       []string{"required", "min:6"},
		"captcha_id":     []string{"required"},
		"captcha_answer": []string{"required", "digits:6"},
	}
	//错误提示
	messages := govalidator.MapData{
		"login_id": []string{
			"required:登录ID为必填项，支持手机号、邮箱和用户名",
			"min:登录ID长度需大于3",
		},
		"password": []string{
			"required:密码必填",
			"min:密码长度大于6",
		},
		"captcha_id": []string{
			"required:图片验证码ID为必填",
		},
		"captcha_answer": []string{
			"required:图片验证码答案必填",
			"digits:图片验证码长度为6位数字",
		},
	}

	errs := validate(data, rules, messages)
	//图片验证码
	_data := data.(*LoginByPasswordRequest)
	errs = validators.ValidateCaptcha(_data.CaptchaID, _data.CaptchaAnswer, errs)

	return errs

}
